/*
 * Filename:    IndicatorConfigController.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年10月10日
 * ex-xieqi001 
 */
package com.paic.mhis.hcpms.quota.indicatorconfig.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.paic.mhis.common.enums.AjaxResponseStatusType;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.quota.indicatorconfig.biz.service.IndicatorConfigService;
import com.paic.mhis.hcpms.quota.indicatorconfig.dto.IndicatorBaseDTO;
import com.paic.mhis.hcpms.quota.indicatorconfig.dto.IndicatorTreeDTO;
import com.paic.mhis.hcpms.quota.indicatorconfig.dto.IndicatorWarnValueDTO;
import com.paic.mhis.hcpms.quota.indicatorconfig.exception.IndicatorConfigException;
import com.paic.mhis.hcpms.quota.indicatorconfig.web.vo.IndicatorConfigVO;
import com.paic.mhis.hcpms.quota.indicatorconfig.web.vo.IndicatorValueVO;
import com.paic.pafa.appclient.annotation.BizServicesProxy;
import com.pingan.um.client.util.StringUtil;

/**
 * 
 * @author EX-XIEQI001
 * 
 */
@Controller
@RequestMapping("/indicatorConfig/")
public class IndicatorConfigController {

	@BizServicesProxy(beanName = "indicatorConfigService")
	private IndicatorConfigService indicatorConfigService;

	// 获取所有可以新增的指标集合
	@RequestMapping("getIndicatorAddData.do")
	@ResponseBody
	public Map<String, Object> getIndicatorAddData(@RequestParam(value = "kpiCode", defaultValue = "") String kpiCode,
			@RequestParam(value = "kpiName", defaultValue = "") String kpiName,
			@RequestParam(value = "currentPage", defaultValue = "0") int currentPage,
			@RequestParam(value = "limit", defaultValue = "20") int limit) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		// 分页开始值
		if (currentPage - 1 <= 0) {
			paramMap.put("minSize", 0);
			paramMap.put("maxSize", limit);
		} else {
			paramMap.put("minSize", (currentPage - 1) * limit);
			paramMap.put("maxSize", currentPage * limit);
		}

		if (StringUtil.isNotEmpty(kpiCode)) {
			paramMap.put("kpiCode", "%" + kpiCode + "%");
		}
		if (StringUtil.isNotEmpty(kpiName)) {
			paramMap.put("kpiName", "%" + kpiName + "%");
		}

		return indicatorConfigService.getIndicatorAddAll(paramMap);
	}

	// 获取所有可以执行的指标
	@RequestMapping("addIndicatorAll.do")
	@ResponseBody
	public Map<String, Object> addIndicatorAll(HttpServletRequest request,
			@RequestParam(value = "regionCode", defaultValue = "") String regionCode) throws IndicatorConfigException {
		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(regionCode)) {
			UserDetails user = (UserDetails) request.getSession().getAttribute("user");
			indicatorConfigService.addIndicatorAll(user.getDisplayName(), regionCode);
			map.put("starts", AjaxResponseStatusType.Ok);
			map.put("msg", "指标配置完成!!!");
		} else {
			map.put("starts", AjaxResponseStatusType.Error);
			map.put("msg", "统筹区不能为空");
		}

		return map;
	}

	// 获取所有可以执行的指标
	@RequestMapping("addIndicator.do")
	@ResponseBody
	public Map<String, Object> addIndicator(@ModelAttribute IndicatorConfigVO vo, HttpServletRequest request)
			throws IndicatorConfigException {

		Map<String, Object> map = new HashMap<String, Object>();
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		if (vo != null && vo.getKpiIds() != null && vo.getKpiIds().size() > 0) {
			indicatorConfigService.addIndicators(vo.getKpiIds(), user.getUsername(), vo.getRegionCode());
			map.put("starts", AjaxResponseStatusType.Ok.toString());
			map.put("msg", "新增成功!!!");
		} else {
			map.put("msg", "新增指标为空");
		}

		return map;
	}

	// 获取所有可以执行的指标
	@RequestMapping("getIndicatorData.do")
	@ResponseBody
	public Map<String, Object> getIndicatorData(@ModelAttribute IndicatorConfigVO vo) {

		Map<String, Object> map = new HashMap<String, Object>();
		List<IndicatorBaseDTO> list = indicatorConfigService.getIndicatorBaseAll(vo.getRegionCode());
		map.put("list", list);
		return map;
	}

	// 获取所有统筹区下指标
	@RequestMapping("getIndicatorTree.do")
	@ResponseBody
	public Map<String, Object> getIndicatorTree(HttpServletRequest request) {
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		Map<String, Object> map = new HashMap<String, Object>();
		List<IndicatorTreeDTO> list = indicatorConfigService.getIndicatorTreeAll(user.getUsername());
		map.put("list", list);
		return map;
	}

	// 删除指标
	@RequestMapping("deleteIndicator.do")
	@ResponseBody
	public Map<String, Object> deleteIndicator(@ModelAttribute IndicatorConfigVO vo, HttpServletRequest request)
			throws IndicatorConfigException {
		Map<String, Object> map = new HashMap<String, Object>();
		if (vo != null && StringUtil.isNotEmpty(vo.getKpiId())) {
			UserDetails user = (UserDetails) request.getSession().getAttribute("user");
			indicatorConfigService.deleteIndicator(vo.getKpiId(), user.getUsername(), vo.getRegionCode());
			map.put("starts", AjaxResponseStatusType.Ok.toString());
			map.put("msg", "删除成功!!!");
		} else {
			map.put("msg", "请选中一个指标进行删除操作");
		}
		return map;
	}

	// 获取指标详细信息
	@RequestMapping("getIndicatorsInfo.do")
	@ResponseBody
	public Map<String, Object> getIndicatorsInfo(@ModelAttribute IndicatorConfigVO vo) throws IndicatorConfigException {
		Map<String, Object> map = new HashMap<String, Object>();
		if (vo != null && StringUtil.isNotEmpty(vo.getKpiId())) {
			IndicatorBaseDTO dto = indicatorConfigService.getIndicatorsInfo(vo.getKpiId(), vo.getRegionCode());
			map.put("starts", AjaxResponseStatusType.Ok.toString());
			map.put("info", dto);

		} else {
			map.put("msg", "请选中一个指标进行操作");
		}
		return map;
	}

	// 修改指标信息
	@RequestMapping("updateIndicatorsInfo.do")
	@ResponseBody
	public Map<String, Object> updateIndicatorsInfo(@ModelAttribute IndicatorConfigVO vo, HttpServletRequest request)
			throws IndicatorConfigException {
		Map<String, Object> map = new HashMap<String, Object>();
		if (vo != null && StringUtil.isNotEmpty(vo.getKpiId())) {
			UserDetails user = (UserDetails) request.getSession().getAttribute("user");
			indicatorConfigService.updateIndicatorInfo(vo.getKpiTypes(), vo.getOrgTypes(), vo.getShowType(),
					vo.getVisitType(), vo.getIsValid(), vo.getKpiId(), user.getUsername(), vo.getRegionCode());
			map.put("starts", AjaxResponseStatusType.Ok.toString());
			map.put("msg", "修改成功");
		} else {
			map.put("msg", "请选中一个指标进行操作");
		}
		return map;
	}

	// 修改指标阈值信息
	@RequestMapping("updateIndicatorsValue.do")
	@ResponseBody
	public Map<String, Object> updateIndicatorsValue(@ModelAttribute IndicatorValueVO vo, HttpServletRequest request)
			throws IndicatorConfigException {
		Map<String, Object> map = new HashMap<String, Object>();
		String msg = "";
		if (vo != null && StringUtils.isBlank(vo.getId())) {
			msg = "请选择一个规则阈值修改";
		}
		if (vo != null && StringUtils.isBlank(vo.getType())) {
			msg = "规则阈值的类型不能为空";
		}
		if (vo != null && StringUtils.isBlank(vo.getValue())) {
			msg = "规则阈值的值不能为空";
		} else if (vo != null && !vo.getValue().matches("^[1-9]{1}[0-9]*|[0]$")) {
			msg = "阈值必须为数值";
		}
		if (vo != null && StringUtil.isEmpty(vo.getKpiId())) {
			msg = "请选中一个指标阈值进行操作";
		}

		if (StringUtils.isBlank(msg)) {
			if ("2".equals(vo.getType())) {
				double i = Double.parseDouble(vo.getValue());
				double d = i / 100;
				vo.setValue(d + "");
			}
			UserDetails user = (UserDetails) request.getSession().getAttribute("user");
			indicatorConfigService.updateIndicatorValue(vo.getId(), vo.getKpiId(), vo.getValue(), user.getUsername(),
					vo.getRegionCode());
			map.put("starts", AjaxResponseStatusType.Ok.toString());
			msg = "修改成功";
		}
		map.put("msg", msg);
		return map;
	}

	// 修改指标预警规则信息
	@RequestMapping("updateIndicatorsWarnValue.do")
	@ResponseBody
	public Map<String, Object> updateIndicatorsWarnValue(@ModelAttribute IndicatorConfigVO vo,
			HttpServletRequest request) throws IndicatorConfigException {
		Map<String, Object> map = new HashMap<String, Object>();
		String msg = "";
		if (vo != null && StringUtils.isBlank(vo.getId())) {
			msg = "请选择一个预警规则阈值修改";
		}
		if (vo != null && StringUtil.isEmpty(vo.getKpiId())) {
			msg = "请选中一个指标预警阈值进行操作";
		}

		if (StringUtils.isBlank(msg)) {
			UserDetails user = (UserDetails) request.getSession().getAttribute("user");
			IndicatorWarnValueDTO dto = new IndicatorWarnValueDTO();
			BeanUtils.copyProperties(vo, dto);
			indicatorConfigService.updateIndicatorsWarnValue(user.getUsername(), dto);
			map.put("starts", AjaxResponseStatusType.Ok.toString());
			msg = "修改成功";
		}
		map.put("msg", msg);
		return map;
	}

	// 重新计算指标预警规则信息
	@RequestMapping("calIndicatorsWarnValues.do")
	@ResponseBody
	public Map<String, Object> calIndicatorsWarnValues(@ModelAttribute IndicatorConfigVO vo, HttpServletRequest request)
			throws IndicatorConfigException {
		Map<String, Object> map = new HashMap<String, Object>();
		String msg = "";
		if (vo != null && StringUtils.isBlank(vo.getId())) {
			msg = "请选择一个预警规则阈值修改";
		}
		if (vo != null && StringUtil.isEmpty(vo.getKpiId())) {
			msg = "请选中一个指标预警阈值进行操作";
		}

		if (StringUtils.isBlank(msg)) {
			IndicatorWarnValueDTO dto = new IndicatorWarnValueDTO();
			BeanUtils.copyProperties(vo, dto);
			map = indicatorConfigService.calKpiValues(vo.getValueType(), dto);
			map.put("starts", AjaxResponseStatusType.Ok.toString());
			msg = "修改成功";
		}
		map.put("msg", msg);
		return map;
	}
	// 重新计算指标预警规则信息
		@RequestMapping("numOfOrg.do")
		@ResponseBody
		public Map<String, Object> numOfOrg(HttpServletRequest request)
			 {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("regionCode",request.getParameter("regionCode"));
			map.put("orgLevel", request.getParameter("orgLevel"));
			map.put("count", indicatorConfigService.numOfOrg(map));
			return map;
		}
}
